BTSTACK_ROOT = ../../..

prefix = @prefix@

CC = @CC@
LDFLAGS = @LDFLAGS@
CFLAGS = @CFLAGS@ \
    -I $(BTSTACK_ROOT)/platform/daemon/src \
    -I $(BTSTACK_ROOT)/platform/posix \
    -I $(BTSTACK_ROOT)/src \
    -I..
BTSTACK_LIB_LDFLAGS = @BTSTACK_LIB_LDFLAGS@
BTSTACK_LIB_EXTENSION = @BTSTACK_LIB_EXTENSION@
LIBUSB_CFLAGS = @LIBUSB_CFLAGS@
LIBUSB_LDFLAGS = @LIBUSB_LDFLAGS@

VPATH += ${BTSTACK_ROOT}/platform/daemon/src
VPATH += ${BTSTACK_ROOT}/platform/corefoundation
VPATH += ${BTSTACK_ROOT}/platform/libusb
VPATH += ${BTSTACK_ROOT}/platform/posix
VPATH += ${BTSTACK_ROOT}/src
VPATH += ${BTSTACK_ROOT}/src/ble
VPATH += ${BTSTACK_ROOT}/src/classic

remote_device_db_sources = @REMOTE_DEVICE_DB_SOURCES@
btstack_run_loop_sources = @btstack_run_loop_SOURCES@
usb_sources = @USB_SOURCES@

libBTstack_SOURCES =    \
    btstack.c           \
    socket_connection.c \
    hci_dump.c          \
    hci_cmd.c          \
    daemon_cmds.c       \
    btstack_linked_list.c    \
    btstack_run_loop.c  \
    sdp_util.c          \
    spp_server.c        \
    btstack_util.c             \
    $(btstack_run_loop_sources) \
			  
BTdaemon_SOURCES =      \
    $(libBTstack_SOURCES)       \
    $(usb_sources)              \
    $(remote_device_db_sources) \
    ad_parser.c                 \
    att_db.c                    \
    att_dispatch.c              \
    att_server.c                \
    bnep.c                      \
    btstack_memory.c            \
    btstack_memory_pool.c       \
    btstack_tlv.c               \
    btstack_uart_block_posix.c  \
    daemon.c                    \
    gatt_client.c               \
    hci.c                       \
    hci_dump.c                  \
    hci_transport_h4.c          \
    l2cap.c                     \
    l2cap_signaling.c           \
    le_device_db_memory.c       \
    rfcomm.c                    \
    sdp_client.c                \
    sdp_client_rfcomm.c         \
    sdp_server.c                \
    sm.c                        \

# use $(CC) for Objective-C files
.m.o:
	$(CC) $(CFLAGS) -c -o $@ $<

# libBTstack.a
all: libBTstack.$(BTSTACK_LIB_EXTENSION) BTdaemon

libBTstack.$(BTSTACK_LIB_EXTENSION): $(libBTstack_SOURCES)
		$(BTSTACK_ROOT)/tool/get_version.sh
		$(CC) $(CFLAGS) $(BTSTACK_LIB_LDFLAGS) -o $@ $^ $(LDFLAGS)

# libBTstack.a: $(libBTstack_SOURCES:.c=.o) $(libBTstack_SOURCES:.m=.o)
#		ar cru $@ $(libBTstack_SOURCES:.c=.o) $(libBTstack_SOURCES:.m=.o)
#		ranlib $@

BTdaemon: $(BTdaemon_SOURCES)
		$(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS) $(LIBUSB_CFLAGS) $(LIBUSB_LDFLAGS)

clean:
	rm -rf libBTstack* BTdaemon *.o
	
install:    
	echo "Installing BTdaemon in $(prefix)..."
	mkdir -p $(prefix)/bin $(prefix)/lib $(prefix)/include
	# cp libBTstack.a $(prefix)/lib/
	cp libBTstack.dylib $(prefix)/lib/
	cp BTdaemon $(prefix)/bin/
	cp -r $(BTSTACK_ROOT)/include/btstack $(prefix)/include
